/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package qs.crm.modules.salChance.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import qs.crm.modules.salChance.domain.SalChance;
import qs.crm.modules.salChance.repository.SalChanceRepository;
import qs.crm.modules.salChance.service.SalChanceService;
import qs.crm.modules.salChance.service.dto.SalChanceDto;
import qs.crm.modules.salChance.service.dto.SalChanceQueryCriteria;
import qs.crm.modules.salChance.service.mapstruct.SalChanceMapper;
import qs.crm.utils.FileUtil;
import qs.crm.utils.PageUtil;
import qs.crm.utils.QueryHelp;
import qs.crm.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author tong
* @date 2023-05-26
**/
@Service
@RequiredArgsConstructor
public class SalChanceServiceImpl implements SalChanceService {

    private final SalChanceRepository salChanceRepository;
    private final SalChanceMapper salChanceMapper;

    @Override
    public Map<String,Object> queryAll(SalChanceQueryCriteria criteria, Pageable pageable){
        Page<SalChance> page = salChanceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(salChanceMapper::toDto));
    }

    @Override
    public List<SalChanceDto> queryAll(SalChanceQueryCriteria criteria){
        return salChanceMapper.toDto(salChanceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SalChanceDto findById(Long chcId) {
        SalChance salChance = salChanceRepository.findById(chcId).orElseGet(SalChance::new);
        ValidationUtil.isNull(salChance.getChcId(),"SalChance","chcId",chcId);
        return salChanceMapper.toDto(salChance);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SalChanceDto create(SalChance resources) {
        return salChanceMapper.toDto(salChanceRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SalChance resources) {
        SalChance salChance = salChanceRepository.findById(resources.getChcId()).orElseGet(SalChance::new);
        ValidationUtil.isNull( salChance.getChcId(),"SalChance","id",resources.getChcId());
        salChance.copy(resources);
        salChanceRepository.save(salChance);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long chcId : ids) {
            salChanceRepository.deleteById(chcId);
        }
    }

    @Override
    public void download(List<SalChanceDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SalChanceDto salChance : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("机会来源", salChance.getChcSource());
            map.put("客户名称", salChance.getChcCustName());
            map.put("机会概要", salChance.getChcTitle());
            map.put("成功率", salChance.getChcRate());
            map.put("联系人", salChance.getChcLinkman());
            map.put("联系电话", salChance.getChcTel());
            map.put("机会描述", salChance.getChcDesc());
            map.put("创建机会编号", salChance.getChcCreateId());
            map.put("机会创建人", salChance.getChcCreateBy());
            map.put("创建时间", salChance.getChcCreateDate());
            map.put("机会处理编号", salChance.getChcDueId());
            map.put("处理人", salChance.getChcDueTo());
            map.put("处理时间", salChance.getChcDueDate());
            map.put("处理状态", salChance.getChcStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}